package com.symaster.pdfcompose.frame.component.pdfpanel;

import com.symaster.common.swing.util.SwingUtil;
import com.symaster.pdfcompose.frame.component.pdfrow.PdfRow;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;

/**
 * @author yinmiao
 * @version 2024/4/29
 */
public class ResultItem extends SourceItem {

    private final JLabel label = new JLabel("处理中");
    private final JLabel notFountLabel = new JLabel("文件不存在");
    private final JLabel filLabel = new JLabel("失败");
    private Status status = Status.RESULT;
    private Component[] components1;
    private JComponent rightBar;
    private final Runnable deleteRun;

    public ResultItem(JComponent component, Runnable deleteRun) {
        super(component);
        this.deleteRun = deleteRun;
    }

    @Override
    OptionBar getOptionBar() {
        return null;
    }

    @Override
    JComponent getRightBar() {
        rightBar = super.getRightBar();

        Component[] components = rightBar.getComponents();

        components1 = new Component[components.length + 1];
        components1[0] = new JButton(SwingUtil.loadIconEnabledCache("pdf-folder.png", 14, 14));
        JButton jButton = (JButton) components1[0];
        jButton.addActionListener(e -> {
            File pdfFile = getFile();

            if (pdfFile != null) {
                try {
                    SwingUtil.openFolder(pdfFile);
                } catch (IOException ignored) {
                }
            }

        });

        System.arraycopy(components, 0, components1, 1, components.length);

        rightBar.removeAll();
        for (Component component : components1) {
            rightBar.add(component);
        }

        return rightBar;
    }

    private File getFile() {
        File pdfFile = null;
        JComponent component = getComponent();
        if (component instanceof PdfRow) {
            PdfRow pd = (PdfRow) component;
            pdfFile = pd.getPdfFile();
        }
        return pdfFile;
    }

    @Override
    JButton getDelBtn() {
        JButton delBtn = super.getDelBtn();
        delBtn.addActionListener(e -> {
            File file = getFile();
            if (file.isFile() && file.delete()) {
                System.out.println("文件删除");
            }
            if (deleteRun != null) {
                deleteRun.run();
            }
        });

        return delBtn;
    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;

        SwingUtilities.invokeLater(() -> {
            rightBar.setVisible(false);
            rightBar.removeAll();
            if (Status.EXEC.equals(status)) {
                rightBar.add(label);
            } else if (Status.NOT_FOUNT.equals(status)) {
                rightBar.add(notFountLabel);
                for (int i = 1; i < components1.length; i++) {
                    rightBar.add(components1[i]);
                }
            } else if (Status.RESULT.equals(status)) {
                for (Component component : components1) {
                    rightBar.add(component);
                }
            } else if (Status.FIL.equals(status)) {
                rightBar.add(filLabel);
            }
            rightBar.setVisible(true);
        });
    }

    public enum Status {
        EXEC,
        RESULT,
        NOT_FOUNT,
        FIL
    }
}
